package com.kfm.shop.controller;

import com.kfm.shop.domain.Order;
import com.kfm.shop.domain.Product;
import com.kfm.shop.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class OrderController {

    @Resource
    RestTemplate restTemplate;

    @Resource
    DiscoveryClient discoveryClient;

    @RequestMapping("/test")
    public String test() {
        return "order";
    }

    @RequestMapping("/prod/{uid}/{pid}")
    public Order prod(@PathVariable("uid") Integer uid, @PathVariable("pid") Integer pid) {
        // TODO 去 user 的微服务中 查询 用户信息
        // 发送 http://localhost:8070/user/find/uid 查询用户信息
        // 通过注册中心获取 服务 user-service
        ServiceInstance serviceInstance = discoveryClient.getInstances("user-service").get(0);
        URI uri = serviceInstance.getUri();
        log.info("查询用户{}信息", uri);
        User user = restTemplate.getForObject(uri + "/user/find/" + uid, User.class);
        // User user = restTemplate.getForObject("http://localhost:8070/user/find/" + uid, User.class);
        log.info("查询用户{}信息成功,信息为{}", uid, user);
        log.info("查询商品{}信息", pid);
        ServiceInstance serviceInstance1 = discoveryClient.getInstances("product-service").get(0);
        Product product = restTemplate.getForObject(serviceInstance1.getUri() + "/product/find/" + pid, Product.class);
        // Product product = restTemplate.getForObject("http://localhost:8080/product/find/" + pid, Product.class);
        log.info("查询商品{}信息成功,信息为{}", pid, product);
        if (product.getRepertory() < 0) {
            log.error("无法下单");
            return null;
        }
        Order order = new Order();
        order.setId(1);
        order.setPid(1);
        order.setUid(user.getId());
        order.setUsername(user.getUsername());
        order.setPrice(product.getPrice());
        order.setNumber(1);
        log.info("创建订单成功，订单信息{}", order);
        return order;

    }

    @RequestMapping("/prod2/{uid}/{pid}")
    public void prod2(@PathVariable("uid") Integer uid, @PathVariable("pid") Integer pid) {
        // List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
        // // instances.forEach(o -> System.out.println(o.getUri()));
        // // 随机选择一个服务
        // ServiceInstance serviceInstance = instances.get(new Random().nextInt(instances.size()));
        // URI uri = serviceInstance.getUri();
        // System.out.println(uri);
        // Product product = restTemplate.getForObject(uri + "/product/find/" + pid, Product.class);
        // System.out.println(product);
        // ServiceInstance instance = discoveryClient.getInstances("user-service").get(0);
        // User user = restTemplate.getForObject(instance.getUri() + "/user/find/" + uid, User.class);
        // System.out.println(user);
        // 开启 负载均衡之后 可以 直接使用 服务名 替代 ip+端口 http://localhost:8080  -->
        // "http://" + instance.getHost() +":" + instance.getPort()
        // product-service 底层会被替代成 IP:端口
        Product forObject = restTemplate.getForObject("http://product-service/product/find/1", Product.class);
        System.out.println(forObject);
    }
}
